var select_node_id = '';
var goods = [];
var zNodesNo = [];
var zNodesYes = [];
$(function(){
	var tree_data = AjaxUtil.command.executeQueryTransaction([
		{sqlID:"sjc_shop_goods_classify_1", parameters: {shop_id: shop_id}},
		{sqlID:"sjc_shop_goods_classify_6", parameters: {shop_id: shop_id}}]);
	if(AjaxUtil.command.isSucceed(tree_data)) {
		zNodes = tree_data.rows[0];
		goods = tree_data.rows[1];
	}
	zNodes.push({id:'0', name:'分类', pid:'-1'});
	$.fn.zTree.init($("#treeClassify"), setting, zNodes);
	
	initGoodsTree();
});

function initGoodsTree() {
	zNodesNo = getGoodsNodes(null);
	$.fn.zTree.init($("#treeGoodsNo"), setting1, zNodesNo);
	if(select_node_id == '') {
		$.fn.zTree.init($("#treeGoodsYes"), setting1);
	} else {
		zNodesYes = getGoodsNodes(select_node_id);
		$.fn.zTree.init($("#treeGoodsYes"), setting1, zNodesYes);
	}
}

function getGoodsNodes(cls_id) {
	var list = [];
	for(var i=0; i<goods.length; i++) {
		if(goods[i].cls_id == cls_id) list.push(goods[i]);
	}
	return list;
}

var setting = {
	view: {
		addHoverDom: addHoverDom,
		removeHoverDom: removeHoverDom
	},
	edit: {
		enable: true,
		showRemoveBtn: showRemoveBtn,
		showRenameBtn: showRenameBtn,
		drag: {
			prev: isPrev,
			next: isNext,
			inner: isInner
		}
	},
	data: {
		simpleData: {
			enable: true,
			pIdKey: "pid"
		}
	},
	callback: {
		beforeDrag: beforeDrag,
		beforeDrop: beforeDrop,
		beforeRemove: beforeRemove,
		onDrop: onDrop,
		onRename: onRename,
		onClick: onClick
	}
};

var zNodes =[];

function showRemoveBtn(treeId, treeNode) {
	return !(treeNode.id=='0');
}

function showRenameBtn(treeId, treeNode) {
	return !(treeNode.id=='0');
}

function isPrev(treeId, treeNodes, targetNode) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].mark == "1") return false;
	}
	return !(!targetNode!=null || targetNode.id=='0');
}

function isInner(treeId, treeNodes, targetNode) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].mark == "1") return false;
	}
	return targetNode!=null;
}

function isNext(treeId, treeNodes, targetNode) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].mark == "1") return false;
	}
	return !(!targetNode!=null || targetNode.id=='0');
}

function beforeDrag(treeId, treeNodes) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].id=='0') return false;
	}
	return true;
}

function beforeDrop(treeId, treeNodes, targetNode, moveType) {
	return targetNode ? targetNode.drop !== false : true;
}

function beforeRemove(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeClassify");
	zTree.selectNode(treeNode);
	if(confirm("确认删除 节点 -- " + treeNode.name + " 吗？")) {
		if(!AjaxUtil.command.isSucceed(AjaxUtil.command.executeUpdate("sjc_shop_goods_classify_4", {cls_id:treeNode.id}))){
			$.jqalert("删除失败！");
		} else {
			if(select_node_id == treeNode.id) {
				$("#td_classify").text("归入【  】");
				select_node_id = "";
			}
			return true;
		}
	}
	return false;
}

function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
		+ "' title='add node' onfocus='this.blur();'></span>";
	sObj.after(addStr);
	var btn = $("#addBtn_"+treeNode.tId);
	if (btn) btn.bind("click", function(){
		var zTree = $.fn.zTree.getZTreeObj("treeClassify");
		var cls_id = (new UUID()).toString();
		var cls_name = "新节点";
		if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeUpdate("sjc_shop_goods_classify_2", {
			cls_id:cls_id, shop_id:shop_id, cls_name:cls_name, pid:treeNode.id
		}))){
			zTree.addNodes(treeNode, {id:cls_id, pid:treeNode.id, name:cls_name});
		} else {
			$.jqalert("添加失败！");
		}
		return false;
	});
};
function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.tId).unbind().remove();
}

function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
	if(treeId=="treeClassify" && moveType!=null && targetNode) {
		var arrSql = [];
		for(var i=0; i<treeNodes.length; i++) {
			arrSql.push({
				sqlID: 'sjc_shop_goods_classify_5',
				parameters: {
					cls_id: treeNodes[i].id,
					pid: targetNode.id
				}
			});
		}
		
		if(!AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction(arrSql))) {
			$.jqalert("移动失败！");
		}
	}
}

function onRename(event, treeId, treeNode, isCancel) {
	if(!AjaxUtil.command.isSucceed(AjaxUtil.command.executeUpdate("sjc_shop_goods_classify_3", {
		cls_id:treeNode.id, cls_name:treeNode.name
	}))){
		$.jqalert("修改失败！");
	}
}

function onClick(event, treeId, treeNode) {
	if(treeNode.id == '0') {
		$("#td_classify").text("归入【  】");
		select_node_id = "";
	} else {
		$("#td_classify").text("归入【 " + treeNode.name + " 】");
		select_node_id = treeNode.id;
	}
	initGoodsTree();
}


var setting1 = {
	edit: {
		enable: true,
		showRemoveBtn: false,
		showRenameBtn: false,
		drag: {
			prev: isPrev1,
			next: isNext1,
			inner: isInner1
		}
	},
	data: {
		simpleData: {
			enable: true,
			pIdKey: "pid"
		}
	},
	callback: {
		beforeDrag: beforeDrag1,
		beforeDrop: beforeDrop1,
		onDrop: onDrop1
	}
};

function isPrev1(treeId, treeNodes, targetNode) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].mark!="1" || select_node_id==""
			|| (treeNodes[i].cls_id==null && treeId=='treeGoodsNo') 
			|| (treeNodes[i].cls_id!=null && treeId=='treeGoodsYes')) return false;
	}
	return true;
}

function isInner1(treeId, treeNodes, targetNode) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].mark!="1"  || select_node_id==""
			|| (treeNodes[i].cls_id==null && treeId=='treeGoodsNo') 
			|| (treeNodes[i].cls_id!=null && treeId=='treeGoodsYes')) return false;
	}
	return true;
}

function isNext1(treeId, treeNodes, targetNode) {
	for(var i=0; i<treeNodes.length; i++) {
		if(treeNodes[i].mark!="1" || select_node_id==""
			|| (treeNodes[i].cls_id==null && treeId=='treeGoodsNo') 
			|| (treeNodes[i].cls_id!=null && treeId=='treeGoodsYes')) return false;
	}
	return true;
}

function beforeDrag1(treeId, treeNodes) {
	for (var i=0,l=treeNodes.length; i<l; i++) {
		if (treeNodes[i].drag === false) {
			return false;
		}
	}
	return true;
}

function beforeDrop1(treeId, treeNodes, targetNode, moveType) {
	return targetNode ? targetNode.drop !== false : true;
}

function onDrop1(event, treeId, treeNodes, targetNode, moveType, isCopy) {
	if(treeId!="treeClassify" && moveType!=null && select_node_id!="") {
		var arrSql = [];
		var common = "sjc_shop_goods_classify_8";
		if(treeId == "treeGoodsYes") {
			common = "sjc_shop_goods_classify_7";
		}
		
		for(var i=0; i<treeNodes.length; i++) {
			arrSql.push({
				sqlID: common,
				parameters: {
					goods_id: treeNodes[i].id,
					cls_id: select_node_id
				}
			});
			if(treeId == "treeGoodsYes") {
				treeNodes[i].cls_id = select_node_id;
				setGoodsNodeValue(treeNodes[i].id, select_node_id);
			} else {
				treeNodes[i].cls_id = null;
				setGoodsNodeValue(treeNodes[i].id, null);
			}
		}
		
		if(!AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction(arrSql))) {
			$.jqalert("移动失败！");
		}
	}
}

function setGoodsNodeValue(id, value) {
	for(var i=0; i<goods.length; i++) {
		if(goods[i].id == id) {
			goods[i].cls_id = value;
		}
	}
}